﻿Imports Publics
Imports System.Windows.Forms
Imports Vla.Vms

Public Class FrmCapNhatTaiKhoan_NguoiSuDung
    Private cls As New ClsDesEncrypt
    Private _ID As String = ""
    Public Property ID() As String
        Get
            Return _ID
        End Get
        Set(ByVal value As String)
            _ID = value
        End Set
    End Property

    Private _objNguoiDung As DM_NhanVien
    Public Property objNguoiDung() As DM_NhanVien
        Get
            Return _objNguoiDung
        End Get
        Set(ByVal value As DM_NhanVien)
            _objNguoiDung = value
        End Set
    End Property

    Private _ActionForm As String
    Public Property ActionForm() As String
        Get
            Return _ActionForm
        End Get
        Set(ByVal value As String)
            _ActionForm = value
        End Set

    End Property
    Private Function CheckSave() As String
        Dim strErr As String = ""
        If String.IsNullOrEmpty(txtMaNguoiDung.Text) Then
            txtMaNguoiDung.Focus()
            Return "Chưa nhập mã người dùng"
        ElseIf String.IsNullOrEmpty(txtHoTen.Text) Then
            txtHoTen.Focus()
            Return "Chưa nhập họ tên"
        ElseIf String.IsNullOrEmpty(txtMaDonVi_PhongBan.Text) Then
            txtMaDonVi_PhongBan.Focus()
            Return "Chưa nhập mã đơn vị"
        Else
            Select Case ActionForm
                Case vAddAction
                    Dim obj As DM_NhanVien = (From ds In dbTV.DM_NhanViens Where ds.InUsed _
                                              And ds.MaSo = txtMaNguoiDung.Text Select ds).FirstOrDefault
                    If obj IsNot Nothing Then
                        txtMaNguoiDung.Focus()
                        Return "Trùng mã nhân viên"
                    End If
                    Dim objCheck As DM_NhanVien = (From ds In dbTV.DM_NhanViens Where ds.InUsed _
                                              And ds.UserName.ToLower = txtUserName.Text.Trim.ToLower _
                                        Select ds).FirstOrDefault
                    If objCheck IsNot Nothing Then
                        txtUserName.Focus()
                        Return "Tài khoản đăng nhập đã tồn tại"
                    End If
                Case vEditAction
                    If objNguoiDung IsNot Nothing Then
                        If txtMaNguoiDung.Text.Trim <> objNguoiDung.MaSo.Trim Then
                            Dim obj As DM_NhanVien = (From ds In dbTV.DM_NhanViens Where ds.InUsed _
                                                        And ds.MaSo = txtMaNguoiDung.Text AndAlso ds.ID <> objNguoiDung.ID _
                                                     Select ds).FirstOrDefault
                            If obj IsNot Nothing Then
                                txtMaNguoiDung.Focus()
                                Return "Trùng mã nhân viên"
                            End If
                        End If
                        '
                        Dim objCheck As DM_NhanVien = (From ds In dbTV.DM_NhanViens Where ds.InUsed _
                                             And ds.UserName.ToLower = txtUserName.Text.Trim.ToLower _
                                             AndAlso ds.ID <> objNguoiDung.ID _
                                       Select ds).FirstOrDefault
                        If objCheck IsNot Nothing Then
                            txtUserName.Focus()
                            Return "Tài khoản đăng nhập đã tồn tại"
                        End If
                    End If
            End Select
        End If
        Return strErr
    End Function
     
    Private Sub GetCaptionForm()
        Dim strGhiVaDong As String = String.Empty
        Dim strThoat As String = GetStrKey(kThoat)

        If BtnGhiVaDong.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And BtnGhiVaDong.Enabled = True Then
            strGhiVaDong = GetStrKey(kGhiVaDong)
        End If

        'If btnTroGiup.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And btnTroGiup.Enabled = True Then
        '    strTroGiup = GetStrKey(kTroGiup)
        'End If
        Me.Text = Me.Text + strGhiVaDong + strThoat 'strTroGiup + 
    End Sub

    Public Sub SetMaNguoiSuDung()
        Dim iMAX As Integer = 0
        Try
            iMAX = (From ds In dbTV.DM_NhanViens Where ds.InUsed Select Convert.ToInt32(ds.MaSo.Substring(2, ds.MaSo.Length - 1))).Max
        Catch ex As Exception
            iMAX = 0
        End Try
        Dim MaSo As String = "NV"
        For i = 0 To 3 - (iMAX + 1).ToString.Length
            MaSo &= "0"
        Next
        txtMaNguoiDung.Text = MaSo & (iMAX + 1).ToString
    End Sub

    'Public Function SinhMaNSD(ByVal sPrefix As String, Optional ByVal cCharNum As Integer = 3) As String
    '    Try
    '        Dim sFormatStr As String = ""
    '        For i As Integer = 1 To cCharNum
    '            sFormatStr = sFormatStr + "0"
    '        Next
    '        Dim objMCB As String

    '        '// Mã người dùng mới nhất
    '        Dim sMCB As String = (From ds In dbTV.DM_NhanViens Where ds.InUsed _
    '                              AndAlso ds.MaSo.Length = sPrefix.Length + cCharNum _
    '                              Order By ds.ID Descending _
    '                              Select ds.MaSo).Take(1).FirstOrDefault

    '        If sMCB IsNot Nothing Then
    '            Dim iPhanSo As Integer = Convert.ToInt32(sMCB.Substring(sMCB.Length - cCharNum, cCharNum))
    '            '// Mã mới
    '            sMCB = sPrefix & (iPhanSo + 1).ToString(sFormatStr)
    '            objMCB = sMCB
    '        Else
    '            objMCB = sPrefix & sFormatStr.Substring(0, sFormatStr.Length - 1) + "1"
    '        End If
    '        Return objMCB
    '    Catch ex As Exception
    '        Return ""
    '    End Try
    'End Function

    Private Sub FrmCapNhatTaiKhoan_NguoiSuDung_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'Ghi và đóng
        If (e.Modifiers = GetKeyModifide(pIdGhiVaDong) And _
        e.KeyCode = GetKeyCode2(pIdGhiVaDong)) Or _
        (e.KeyCode = GetKeyCode(pIdGhiVaDong)) Then
            If BtnGhiVaDong.Enabled = True And BtnGhiVaDong.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnGhiVaDong_ItemClick(Nothing, Nothing)
            End If
        End If

        'Trợ giúp
        If (e.Modifiers = GetKeyModifide(pIdTroGiup) And _
       e.KeyCode = GetKeyCode2(pIdTroGiup)) Or _
       (e.KeyCode = GetKeyCode(pIdTroGiup)) Then
            BtnTroGiup_ItemClick(Nothing, Nothing)
        End If

        'Thoát			
        If (e.Modifiers = GetKeyModifide(pIdThoat) And _
        e.KeyCode = GetKeyCode2(pIdThoat)) Or _
        (e.KeyCode = GetKeyCode(pIdThoat)) Then
            BtnHuyBo_ItemClick(Nothing, Nothing)
        End If
    End Sub

    Private Sub FrmCapNhatTaiKhoan_NguoiSuDung_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SendKeys.Send("{tab}")
        End If
    End Sub

    Private Sub FrmCapNhatTaiKhoan_NguoiSuDung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            BtnGhiVaDong.Caption = BtnSaveAndCloseCaption
            BtnTroGiup.Caption = BtnHelpCaption
            BtnHuyBo.Caption = BtnCloseCaption
            txtHoTen.Focus()
            'txtMaNguoiDung.Properties.ReadOnly = True
            'txtMaNguoiDung.TabStop = False

            Select Case ActionForm
                Case vAddAction
                    Me.Text = "Cập nhật người sử dụng"
                    'txtMaNguoiDung.Text = SinhMaNSD("NV")
                    txtPassWord.Enabled = True
                    SetMaNguoiSuDung()
                Case vEditAction, vCopyAction
                    Me.Text = "Cập nhật người sử dụng"
                    If objNguoiDung IsNot Nothing Then
                        txtMaNguoiDung.Text = objNguoiDung.MaSo
                        txtHoTen.Text = objNguoiDung.HoTen
                        dteNgaySinh.EditValue = objNguoiDung.NgaySinh
                        txtEmail.Text = objNguoiDung.Email
                        txtSoDienThoai.Text = objNguoiDung.DienThoai
                        txtDiaChi.Text = objNguoiDung.DiaChi
                        Dim objPB As DM_DonVi = (From ds In dbTV.DM_DonVis Where ds.InUsed _
                                                 And ds.ID = objNguoiDung.ID_DonVi Select ds).FirstOrDefault
                        If objPB IsNot Nothing Then
                            txtMaDonVi_PhongBan.Text = objPB.MaDonVi
                            txtMaDonVi_PhongBan.Tag = objPB.ID
                            txtTenDonVi_PhongBan.Text = objPB.TenDonVi
                        End If
                        txtUserName.Text = objNguoiDung.UserName
                        txtPassWord.Text = objNguoiDung.PassWord
                        If objNguoiDung.HanSuDung = "Vĩnh viễn" Then
                            chkTKVinhVien.Checked = True
                            dteNgayHetHan.EditValue = Nothing
                        Else
                            chkTKVinhVien.Checked = False
                            If IsDate(objNguoiDung.HanSuDung) Then
                                dteNgayHetHan.EditValue = CDate(objNguoiDung.HanSuDung)
                            End If
                        End If
                        'If ActionForm = vEditAction Then GroupControl2.Enabled = False
                    End If

            End Select
            GetCaptionForm()
            If ActionForm = vCopyAction Then
                'txtMaNguoiDung.Text = SinhMaNSD("NV")
                SetMaNguoiSuDung()
            End If
        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub BtnGhiVaDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnGhiVaDong.ItemClick
        Try
            Dim str = CheckSave()
            If str <> "" Then
                MsgErr(str)
                Return
            End If

            Select Case ActionForm
                Case vAddAction
                    Dim obj As New DM_NhanVien
                    With obj
                        ID = CreateID()
                        .ID = ID
                        .DM_DonVi = (From ds In dbTV.DM_DonVis Where ds.InUsed AndAlso ds.ID = CStr(txtMaDonVi_PhongBan.Tag) Select ds).FirstOrDefault
                        .MaSo = txtMaNguoiDung.Text
                        .HoTen = txtHoTen.Text
                        .NgaySinh = dteNgaySinh.EditValue
                        .Email = txtEmail.Text
                        .DienThoai = txtSoDienThoai.Text
                        .DiaChi = txtDiaChi.Text
                        .UserName = txtUserName.Text.Trim
                        .PassWord = Vla.Vms.ClsUtil.MD5Encrypt(txtPassWord.Text.Trim)
                        .KichHoat = True
                        If chkTKVinhVien.Checked Then
                            .HanSuDung = "Vĩnh viễn"
                        Else
                            .HanSuDung = ""
                            If dteNgayHetHan.EditValue IsNot Nothing Then
                                .HanSuDung = CDate(dteNgayHetHan.EditValue).ToString("dd/MM/yyyy")
                            End If
                        End If
                        .InUsed = True
                        .EditedOn = CDate(dbTV.GetDateTimeServer)
                    End With

                    dbTV.DM_NhanViens.InsertOnSubmit(obj)
                    iSubmitChange()
                    SaveLog("Thêm mới người dùng", "Thêm mới", "Mã = " & obj.MaSo & "; Tên = " & obj.HoTen, "")
                Case vEditAction
                    Dim obj As DM_NhanVien = (From ds In dbTV.DM_NhanViens Where ds.InUsed _
                                              And ds.ID = objNguoiDung.ID Select ds).FirstOrDefault
                    If obj IsNot Nothing Then
                        With obj
                            ID = obj.ID
                            .DM_DonVi = (From ds In dbTV.DM_DonVis Where ds.InUsed AndAlso ds.ID = CStr(txtMaDonVi_PhongBan.Tag) Select ds).FirstOrDefault
                            .MaSo = txtMaNguoiDung.Text
                            .HoTen = txtHoTen.Text
                            .NgaySinh = dteNgaySinh.EditValue
                            .Email = txtEmail.Text
                            .DienThoai = txtSoDienThoai.Text
                            .DiaChi = txtDiaChi.Text
                            .UserName = txtUserName.Text.Trim
                            '.PassWord = Vla.Vms.ClsUtil.MD5Encrypt(txtPassWord.Text.Trim)
                            .KichHoat = True
                            If chkTKVinhVien.Checked Then
                                .HanSuDung = "Vĩnh viễn"
                            Else
                                .HanSuDung = ""
                                If dteNgayHetHan.EditValue IsNot Nothing Then
                                    .HanSuDung = CDate(dteNgayHetHan.EditValue).ToString("dd/MM/yyyy")
                                End If
                            End If
                            .EditedOn = CDate(dbTV.GetDateTimeServer)
                        End With
                        iSubmitChange()
                        SaveLog("Sửa đổi người dùng", "Sửa đổi", "Mã = " & obj.MaSo & "; Tên = " & obj.HoTen, "")
                    End If
            End Select

            '
            Me.Close()
        Catch ex As Exception

            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub BtnTroGiup_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnTroGiup.ItemClick

    End Sub

    Private Sub BtnHuyBo_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnHuyBo.ItemClick
        Me.Close()
    End Sub

    Private Sub chkTKVinhVien_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If chkTKVinhVien.Checked Then
            dteNgayHetHan.Enabled = False
        Else
            dteNgayHetHan.Enabled = True
        End If
    End Sub

    Private Sub txtMaDonVi_PhongBan_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaDonVi_PhongBan.Leave
        Dim obj As DM_DonVi = (From ds In dbTV.DM_DonVis Where ds.InUsed _
                               And ds.MaDonVi = txtMaDonVi_PhongBan.Text Select ds).FirstOrDefault
        If obj IsNot Nothing Then
            txtMaDonVi_PhongBan.Text = obj.MaDonVi
            txtMaDonVi_PhongBan.Tag = obj.ID
            txtTenDonVi_PhongBan.Text = obj.TenDonVi
            txtUserName.Focus()
        Else
            Dim Frm As New FrmChonDonVi
            Frm.vDataSource = From ds In dbTV.DM_DonVis Where ds.InUsed _
                              Order By ds.ThuTu Select ds
            Frm.ShowDialog()
            If Frm.ObjDonVi IsNot Nothing Then
                txtMaDonVi_PhongBan.Text = Frm.ObjDonVi.MaDonVi
                txtMaDonVi_PhongBan.Tag = Frm.ObjDonVi.ID
                txtTenDonVi_PhongBan.Text = Frm.ObjDonVi.TenDonVi
                txtUserName.Focus()
            Else
                txtMaDonVi_PhongBan.Text = ""
                txtMaDonVi_PhongBan.Tag = Nothing
                txtTenDonVi_PhongBan.Text = ""
            End If
        End If
    End Sub
End Class